// CESIdeology
log using CESIdeology.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\CES24_Common.dta" // Schaffner, Brian; Shih, Marissa; Ansolabehere, Stephen; Pope, Jeremy, 2025, "Cooperative Election Study Common Content, 2024", https://doi.org/10.7910/DVN/X11EP6, Harvard Dataverse, V3. Accessed on 12 April 2025.

// Social Justice scale
*Rename variables
rename CC24_321e defund
rename CC24_444a gendertransition
rename CC24_444b parentalconsentpronouns 
rename CC24_445a raceuniadmissions

*Reverse coding so social justice values are high
foreach var in defund {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in rdefund parentalconsentpronouns gendertransition raceuniadmissions {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in rdefund parentalconsentpronouns gendertransition raceuniadmissions  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Create a liberalism scale
*Rename variables
rename CC24_321a guns
rename CC24_323c wall
rename CC24_324d abortion 
rename CC24_326e oilandgas 
rename CC24_340c samesexmarr 
rename CC24_441b gensofslavery 
rename CC24_440d womenoffended

*Reverse coding so liberal values are high
foreach var in guns abortion oilandgas samesexmarr gensofslavery {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize variables from 1-2, so they're like others in scale
foreach var in rgensofslavery womenoffended {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in rguns wall rabortion roilandgas rsamesexmarr srgensofslavery swomenoffended {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in rguns wall rabortion roilandgas rsamesexmarr srgensofslavery swomenoffended  {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LibValues = .
replace LibValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

//Create left values scale
rename CC24_328d repealaffcare 
rename CC24_328e expandmedicare 
rename CC24_323f forgivestudebt 
rename CC24_341a extendtaxcuts
rename CC24_341b raisecorptax 
rename CC24_341c raiserichtax 
rename CC24_443_1 raisewelfare 
rename CC24_443_2 raisehealthcare 

*Reverse coding so left values are high
foreach var in expandmedicare forgivestudebt raisecorptax raiserichtax raisewelfare raisehealthcare {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize variables from 1-2, so they're like others in scale
foreach var in rraisewelfare rraisehealthcare {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}


* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in repealaffcare rexpandmedicare rforgivestudebt extendtaxcuts rraisecorptax rraiserichtax srraisewelfare srraisehealthcare  {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreLR = 0
gen count_nonzeroLR = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in repealaffcare rexpandmedicare rforgivestudebt extendtaxcuts rraisecorptax rraiserichtax srraisewelfare srraisehealthcare  {
    replace total_scoreLR = total_scoreLR + `var'
    replace count_nonzeroLR = count_nonzeroLR + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LeftValues = .
replace LeftValues = total_scoreLR / count_nonzeroLR if count_nonzeroLR > 0

// Lib-Con selfplacement

replace CC24_330a=. if CC24_330a==8 
egen maxval = max(CC24_330a)
gen libconsp = maxval + 1 - CC24_330a
drop maxval

// Party identification

replace pid7=. if pid7==8
egen maxval = max(pid7)
gen DemID = maxval + 1 - pid7
drop maxval

// Correlation
pwcorr SocJusValues LibValues LeftValues DemID libconsp [aweight=commonweight], sig
log close